import sys
import os
import subprocess
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate

def send_mail(server, from_, to, subject, message):
    msg = MIMEMultipart()
    msg['From'] = from_
    msg['To'] = to
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach(MIMEText(message))
    
    from backups.controllers import logger
    logger.msg('Sending email: from %s to %s via %s, with subject %s.'%(from_, to, server, subject))

    server = smtplib.SMTP(server)
    server.sendmail(from_, to, msg.as_string())
    server.close()
    logger.msg('Email to %s sent.'%from_)